<template>
  <div>
    <van-nav-bar @click-left="$router.go(-1)" :title="$route.name" left-arrow />

    <van-form @submit="register">
      <van-field
        autocomplete="off"
        v-model="regList.phone"
        name="手机号"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[{ required: true, validator, message: '请输入正确手机号' }]"
      />
      <van-field
        autocomplete="off"
        v-model="regList.nickname"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="regList.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { register } from "../util/axios";
import { Toast } from "vant";

export default {
  data() {
    return {
      regList: {
        phone: "",
        nickname: "",
        password: "",
      },
    };
  },
  components: {},
  methods: {
    register() {
      register(this.regList)
      .then((res) => {
        if (res.code == 200) {
          Toast.success(res.msg);
          console.log(res);
          console.log(this.regList);
          this.$router.push("/login");
        } else {
          Toast.fail(res.msg);
        }
      });
    },
    validator(val) {
      return /1\d{10}/.test(val);
    },
  },
};
</script>

<style lang="" scoped>
</style>
